//
//  FindViewController.m
//  Cars' home
//
//  Created by dapeng on 15/11/27.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "FindViewController.h"
#import "Theme-PrefixHeader.pch"
@interface FindViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) NSInteger count;
@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发现";
    self.view.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"ygColor"];
    [self tableViews];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _HUD.color = [UIColor blackColor];
    _HUD.dimBackground = YES;
    _HUD.labelText = @"正在加载";
    [SAPNetWorkTool getWithUrl:findHomePage parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"result"];
        NSDictionary *dic2 = [dic valueForKey:@"apricebuycar"];
        NSDictionary *dic3 = [dic valueForKey:@"discountbuycar"];
        self.oneModel = [[SAPFindOneModel alloc] initWithDataSource:dic2];
        self.threeModel = [[SAPFindThreeModel alloc] initWithDataSource:dic3];
        _HUD.hidden = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    [SAPNetWorkTool getWithUrl:findLunbo parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"result"];
        self.fourModel = [[SAPFindFourModel alloc] initWithDataSource:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    
}
//改变主题的通知
- (void)changeColor {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        self.tabBarController.tabBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        
    }else{
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationPicture"] forBarMetrics:UIBarMetricsDefault];
        self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
        
    }
    
}

- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height - 50) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"tgColor"];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [_tableView registerClass:[SAPFindTableViewCell class] forCellReuseIdentifier:@"SAPFindTableViewCellUITableViewCellIndentifier"];
    [_tableView registerClass:[SAPSecondFindTableViewCell class] forCellReuseIdentifier:@"SAPSecondFindTableViewCellUITableViewCellIndentifier"];
}
#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell  =[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (self.oneModel || self.threeModel) {
    if (indexPath.section == 0) {

    }else if (indexPath.section == 1) {
     SAPFindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPFindTableViewCellUITableViewCellIndentifier"];
        
        cell.threeModel = self.threeModel;
        cell.delegate = self;
        return cell;
    }
    else if (indexPath.section == 2) {
         SAPSecondFindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPSecondFindTableViewCellUITableViewCellIndentifier"];
        cell.oneModel = self.oneModel;
        cell.delegate = self;
        return cell;
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 0;
    }else {
    return self.view.frame.size.width * 2;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 130;
    }else {
    return 30;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    NSMutableArray *pictureArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (SAPFindTwoModel *twoModel in self.fourModel.listArray) {
        [pictureArray addObject:twoModel.imgurl];
        [titleArray addObject:twoModel.title];
    }
    Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 150)];
    [carousel setArray:[NSArray arrayWithArray:pictureArray] withTitArray:titleArray];
    carousel.delegate = self;
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 30)];
    view1.backgroundColor = [UIColor whiteColor];
    UILabel *buyCar = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, 100, 30)];
    buyCar.text = self.threeModel.title;
    buyCar.font = [UIFont systemFontOfSize:12];
    buyCar.textColor = [UIColor grayColor];
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 20, 20)];
    [image1 sd_setImageWithURL:[NSURL URLWithString:self.threeModel.iconurl]];
    [view1 addSubview:image1];
    [view1 addSubview:buyCar];
    
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 30)];
    view2.backgroundColor = [UIColor whiteColor];
    UILabel *buyCar2 = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, 100, 30)];
    buyCar2.text = self.oneModel.title;
    buyCar2.font = [UIFont systemFontOfSize:12];
    buyCar2.textColor = [UIColor grayColor];
    UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 20, 20)];
    [image2 sd_setImageWithURL:[NSURL URLWithString:self.oneModel.iconurl]];
    [view2 addSubview:image2];
    [view2 addSubview:buyCar2];
    
    
    if (section == 0) {
        return carousel;
    }else if (section == 1) {
        return view1;
    }else {
        return view2;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - webViewDelegate
- (void)passCarouselValues:(NSInteger)count {
    SAPCarouseWebViewController *webView = [[SAPCarouseWebViewController alloc] init];
     SAPFindTwoModel *two = self.fourModel.listArray[count];
    webView.murl = two.url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)passFindValue:(NSInteger)count {
    SAPFindWebViewController *webView = [[SAPFindWebViewController alloc] init];
    SAPFindTwoModel *two = self.threeModel.listArray[count];
    webView.murl = two.murl;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)passSecondFindValue:(NSInteger)count {
    SAPSecondWebViewController *webView = [[SAPSecondWebViewController alloc] init];
    SAPFindTwoModel *two1 = self.oneModel.listArray[count];
    webView.murl = two1.murl;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [self changeColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
